from backend.app import db
from datetime import datetime
import json
from sqlalchemy import DECIMAL

class AIPrediction(db.Model):
    """AI预测记录表"""
    __tablename__ = 'ai_predictions'
    
    id = db.Column(db.Integer, primary_key=True)
    start_draw = db.Column(db.String(20), nullable=False, comment='开始期数')
    end_draw = db.Column(db.String(20), nullable=False, comment='结束期数')
    data_count = db.Column(db.Integer, nullable=False, comment='选择数据条数')
    predicted_numbers = db.Column(db.Text, nullable=False, comment='5组预测号码(JSON格式)')
    actual_result = db.Column(db.Text, comment='实际下一期中奖数据(JSON格式)')
    win_info = db.Column(db.Text, comment='中奖信息')
    win_amount = db.Column(DECIMAL(10, 2), default=0, comment='中奖金额')
    analysis_result = db.Column(db.Text, comment='AI分析结果')
    prompt = db.Column(db.Text, comment='发送给AI的prompt')
    ai_model = db.Column(db.String(50), default='deepseek-chat', comment='AI模型类别')
    ai_response = db.Column(db.Text, comment='AI原始返回内容')
    analysis_process = db.Column(db.Text, comment='AI分析过程')
    exclusion_analysis = db.Column(db.Text, comment='互斥关系分析数据(JSON格式)')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    def set_predicted_numbers(self, numbers_list):
        """设置预测号码"""
        self.predicted_numbers = json.dumps(numbers_list, ensure_ascii=False)
    
    def get_predicted_numbers(self):
        """获取预测号码"""
        if self.predicted_numbers:
            return json.loads(self.predicted_numbers)
        return []
    
    def set_actual_result(self, result_dict):
        """设置实际结果"""
        self.actual_result = json.dumps(result_dict, ensure_ascii=False)
    
    def get_actual_result(self):
        """获取实际结果"""
        if self.actual_result:
            return json.loads(self.actual_result)
        return {}
    
    def set_exclusion_analysis(self, exclusion_data):
        """设置互斥关系分析数据"""
        self.exclusion_analysis = json.dumps(exclusion_data, ensure_ascii=False)
    
    def get_exclusion_analysis(self):
        """获取互斥关系分析数据"""
        if self.exclusion_analysis:
            return json.loads(self.exclusion_analysis)
        return {}
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'start_draw': self.start_draw,
            'end_draw': self.end_draw,
            'data_count': self.data_count,
            'predicted_numbers': self.get_predicted_numbers(),
            'actual_result': self.get_actual_result(),
            'win_info': self.win_info,
            'win_amount': float(self.win_amount) if self.win_amount else 0,
            'analysis_result': self.analysis_result,
            'prompt': self.prompt,
            'ai_model': self.ai_model,
            'ai_response': self.ai_response,
            'analysis_process': self.analysis_process,
            'exclusion_analysis': self.get_exclusion_analysis(),
            'created_at': self.created_at.isoformat()
        }
    
    def __repr__(self):
        return f'<AIPrediction {self.start_draw}-{self.end_draw}>' 